package com.emp.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.emp.constant.MySqlEmpConstant;
import com.emp.entity.Emp;

public interface EmpDao {
   //���Ա��
	@Insert("insert into t_emp values (#{empno},"
			+ "#{ename},#{esex},#{eage},#{esalary},#{dept.deptno},#{mgr.empno})")
	void save(Emp emp);
	//ɾ��Ա�� -- ���ݱ��ɾ��
	@Delete(" delete from t_emp where empno=#{empno}")
	void delete(String empno);
  //�޸�Ա����Ϣ
	@Update("update t_emp set ename=#{ename},esex=#{esex},eage=#{eage}, "
			+ " esalary=#{esalary},deptno=#{dept.deptno},mgrno=#{mgr.empno} "
			+ " where empno=#{empno} ")
	void update(Emp emp);
	//��ѯԱ��--���ݱ�Ų�ѯ
	@Select("select e.empno,e.ename,e.esex,e.eage, e.esalary,"
			+ "d.deptno,d.dname,d.location, "
			+ "m.empno mno,m.ename mname  "
			+ "from t_emp e left join t_dept d  "
			+ "on e.deptno = d.deptno left join t_emp m "
			+ "on e.mgrno = m.empno "
			+ "where e.empno=#{empno}")
	@Results(id="empMap",value={
			 /*emp5����������*/
			 @Result(id=true,column="empno",property="empno"),
			 @Result(column="ename",property="ename"),
			 @Result(column="esex",property="esex"),
			 @Result(column="eage",property="eage"),
			 @Result(column="esalary",property="esalary"),
			 /*dept��������*/
			 @Result(column="deptno",property="dept.deptno"),
			 @Result(column="dname",property="dept.dname"),
			 @Result(column="location",property="dept.location"),
			 /*mgr��������*/
             @Result(column="mno",property="mgr.empno"),
             @Result(column="mname",property="mgr.ename")
	})
	Emp queryForEmpno(String empno);
	//==========================================================
	//��ʹ�÷�ҳ����
   //������ҳ��ѯ
	// pageNo ҳ��(�ڼ�ҳ)   pageSize ҳ����(һҳ�м�������)
	// pageCondition ҳ�洫��������
	//xml�в��ܼ��� ��ҵ�����Ӵ���pageNo = (pageNo-1)*pageSize
	@Select(" select e.empno,e.ename,e.esex,e.eage, e.esalary,"
			+ "d.deptno,d.dname,d.location,  "
			+ " m.empno mno,m.ename mname "
			+ " from t_emp e left join t_dept d "
			+ "on e.deptno = d.deptno left join t_emp m  "
			+ "on e.mgrno = m.empno  "
			+ " where INSTR(e.ename,#{pageCondition})>0 limit #{pageNo},#{pageSize} ")
	@ResultMap("empMap")
	List<Emp> queryByPage(@Param("pageNo") Integer pageNo,
                          @Param("pageSize") Integer pageSize,
                          @Param("pageCondition") String pageCondition);
	//������ҳ���ܼ�¼��
	@Select(" select count(0) from t_emp where INSTR(ename,#{pageCondition})>0 ")
	Integer getCount(String pageCondition);
	//==========================================================
	
	//��ѯ���еľ���
	@Select("select DISTINCT m.empno,m.ename from t_emp m inner join t_emp e on m.empno = e.mgrno where m.deptno is not null")
	List<Emp> queryAllMgrs();

   //===============================================================
	//ʹ�÷�ҳ����
	//ֻ��Ҫһ��������ѯ�ķ���
	@Select(" select e.empno,e.ename,e.esex,e.eage, e.esalary,"
			+ "d.deptno,d.dname,d.location,  "
			+ " m.empno mno,m.ename mname "
			+ " from t_emp e left join t_dept d "
			+ "on e.deptno = d.deptno left join t_emp m  "
			+ "on e.mgrno = m.empno  "
			+ " where INSTR(e.ename,#{pageCondition})>0 ")
	@ResultMap("empMap")
	List<Emp> queryByCondition(String pageCondition);
	//===============================================================	
}
